package com.soccertgc.flex.ui.windows
{
	import com.soccertgc.flex.ui.windows.WindowEvent;
	
	import flash.display.DisplayObject;
	
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;
	
	import spark.components.TitleWindow;
	
	[Event(name="windowConfirm", type="com.soccertgc.flex.ui.windows.WindowEvent")]
	[Event(name="windowCancel",  type="com.soccertgc.flex.ui.windows.WindowEvent")]
	public class Window extends TitleWindow implements IWindow
	{
		
		/**
		 * 
		 * 
		 */		
		public function Window()
		{
			super();			
		}
		
		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// Virtual methods
		
		protected function windowOpened():void
		{
		}
		
		protected function windowCanClose():Boolean
		{
			return true;
		}
		
		
		protected function windowClosed():void
		{
		}
		
		// Event dispatchers
		
		protected function dispatchConfirmEvent(data:Object=null):void
		{
			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_CONFIRM, this, data));
		}
		
		protected function dispatchCancelEvent(data:Object=null):void
		{
			dispatchEvent(new WindowEvent(WindowEvent.WINDOW_CANCEL, this, data));
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * 
		 * @param title
		 * @param parent
		 */		
		public function open(windowTitle:String, windowParent:DisplayObject = null):void
		{
			// Title
			title = windowTitle;
			// Parent
			if (windowParent == null) windowParent = FlexGlobals.topLevelApplication as DisplayObject;
			// Shows and centers the window
			PopUpManager.addPopUp(this, windowParent, true);
			PopUpManager.centerPopUp(this);
			// Calls the virtual method windowOpened()
			windowOpened();
		}
		
		/**
		 * 
		 */		
		public function close():void
		{
			// Check if the window can be closed
			if (!windowCanClose()) return;
			
			// Remove the window
			PopUpManager.removePopUp(this);
			// Calls the virtual method windowClosed()
			windowClosed();
		}
	
	}
}